﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAL
{
    public class Category
    {
        public static void insert(CategoryEntity category)
        {
            DbHelper.ExecuteNonQuery("p_Category_insert",
              category.Name,
              category.ParentId,
              category.Treelevel,
              category.Sort,
              category.Shortname,
              category.IsShow
               );
        }

        public static void update(CategoryEntity category)
        {
            DbHelper.ExecuteNonQuery("p_Category_update",
              category.CategoryId,
              category.Name,
              category.ParentId,
              category.Treelevel,
              category.Sort,
              category.Shortname,
              category.IsShow
               );
        }

        public static void delete(int categoryId)
        {
            DbHelper.ExecuteNonQuery("p_Category_delete",
                categoryId);
        }

        public static CategoryEntity getById(int categoryId)
        {
            DataRow dr = DbHelper.ExecuteDataRow("p_category_getById", categoryId);
            CategoryEntity category = new CategoryEntity();
            if (dr != null)
            {
                category.CategoryId = Convert.ToInt32(dr["categoryId"]);
                category.Name = dr["name"].ToString();
                category.ParentId = Convert.ToInt32(dr["parentId"]);
                category.Treelevel = Convert.ToInt32(dr["treelevel"]);
                category.Sort = Convert.ToInt32(dr["sort"]);
                category.Shortname = dr["shortname"].ToString();
                category.IsShow = Convert.ToBoolean(dr["isShow"]);
            }
            return category;
        }

        public static DataTable listAll()
        {
            return DbHelper.ExecuteDataTable("p_Category_listAll");
        }

        public static DataTable getByParent(int parentId)
        {
            return DbHelper.ExecuteDataTable("p_Category_getByParent", parentId);
        }

        public static bool isExistShortname(int categoryId,string shortname)
        {

            if (Convert.ToInt32(DbHelper.ExecuteScalar("p_Category_isExistShortname",categoryId, shortname)) == 1)
                return true;
            else
                return false; 
        }

        public static DataTable listWithCompanyRange(int companyId)
        {
            return DbHelper.ExecuteDataTable("p_Category_listWithCompanyRange", companyId);
        }

        public static DataTable listWithReport(string where)
        {
            return DbHelper.ExecuteDataTable("p_Category_listWithReport",where);
        }

        public static DataTable listWithNewParentByCompany(int companyId)
        {
            return DbHelper.ExecuteDataTable("p_Category_listWithNewParentByCompany", companyId);
        }
        public static DataTable listWithParent()
        {
            return DbHelper.ExecuteDataTable("p_Category_listWithParent");
        }

        public static DataTable listWithPurchaseRoot(int adminId)
        {
            return DbHelper.ExecuteDataTable("p_purchaseChargeSellerWithRootCategory", adminId);
        }
        public static DataTable listWithPurchaseSub(int adminId,int parentCategoryId)
        {
            return DbHelper.ExecuteDataTable("p_PurchaseChargeSellerWithSubCategory",adminId,parentCategoryId);
        }
    }
}
